package com.rzjm.api.netty.charger;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Gjing
 *
 * netty服务初始化器
 **/
@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();
//        pipeline.addLast(new LineBasedFrameDecoder(4096));
        //添加编解码
        pipeline.addLast("decoder", new MyDecoder());
//        pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));

        pipeline.addLast(new NettyServerHandler());
        // 若60s没有收到消息，调用userEventTriggered方法
        pipeline.addLast(new IdleStateHandler(0, 0, SocketPool.HeartSeconds, TimeUnit.SECONDS));
        pipeline.addLast(new HeartBeatHandle());
    }

}
